package pos.gengshen.com.gspos.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;

import com.telpo.tps550.api.TelpoException;
import com.telpo.tps550.api.printer.UsbThermalPrinter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import pos.gengshen.com.gspos.R;
import pos.gengshen.com.gspos.utils.DateTimeUtilT;

/**
 * Created by Administrator on 2018/7/6/006.
 */

public class PrinterActivity extends Activity{


    UsbThermalPrinter mUsbThermalPrinter = new UsbThermalPrinter(PrinterActivity.this);
    List<String> list = new ArrayList<String>();
    Button btn_send;
    String action = "myAction";
    String content = "\n       一键加油       "
            + "\n---------------------------"
            + "\n日期："+DateTimeUtilT.dateTime(new Date())
            + "\n卡号：GS88888888"
            + "\n单号：FP123456789"
            + "\n 项目              小计"
            + "\n-----------------------------"
            + "\n订单总额             56"
            + "\n退款金额             100"
            + "\n实收金额             200"
            + "\n油   品              56"
            + "\n非 油 品             100"
            + "\n异常金额              200"
            + "\n异常笔数               56"
            + "\n送出积分              100"
            + "\n退款笔数              200"
            ;
    ContentPrintThread mThread;
    boolean isStartPrint;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_printer);
        try {
            mUsbThermalPrinter.start(0);
        } catch (TelpoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Button btn = (Button)findViewById(R.id.btn_text);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent();

                intent.setAction(action);
                intent.putExtra("str", content);
                sendOrderedBroadcast(intent, null);

            }
        });
        IntentFilter pIntentFilter = new IntentFilter();
        pIntentFilter.addAction(action);
        pIntentFilter.addAction("android.intent.action.BATTERY_CAPACITY_EVENT");
        registerReceiver(printReceive, pIntentFilter);
    }


    private final BroadcastReceiver printReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String a = intent.getAction();

            if (a.equals(action)) {
                String str = intent.getStringExtra("str");
                list.add(str);
                if (!isStartPrint) {
                    mThread = new ContentPrintThread();
                    mThread.start();
                }

            }
        }
    };




    @Override
    protected void onDestroy(){
        super.onDestroy();
        unregisterReceiver(printReceive);
        mUsbThermalPrinter.stop();
    }


    private class ContentPrintThread extends Thread {

        @Override
        public void run() {
            super.run();
            try {
                while (list.size() > 0) {
                    isStartPrint = true;
                    mUsbThermalPrinter.reset();
                    mUsbThermalPrinter.setLineSpace(0);
                    mUsbThermalPrinter.setAlgin(UsbThermalPrinter.ALGIN_MIDDLE);
                    mUsbThermalPrinter.setFontSize(1);
                    mUsbThermalPrinter.enlargeFontSize(-1, -1);
                    //mUsbThermalPrinter.addString("欢迎光临");
                    mUsbThermalPrinter.setAlgin(UsbThermalPrinter.ALGIN_LEFT);
                    mUsbThermalPrinter.setFontSize(2);
                    mUsbThermalPrinter.enlargeFontSize(1, 1);
                    //mUsbThermalPrinter.addString("---------------------------");
                     mUsbThermalPrinter.addStringOffset(0, "菜名");
//                     mUsbThermalPrinter.addStringOffset(160, "数量");
//                     mUsbThermalPrinter.addStringOffset(240, "数量");
//                     mUsbThermalPrinter.addStringOffset(320, "总额");
                    // mUsbThermalPrinter.endLine();
                    // mUsbThermalPrinter.addStringOffset(0, "狗血12345");
                    // mUsbThermalPrinter.addStringOffset(160, "50");
                    // mUsbThermalPrinter.addStringOffset(240, "数量");
                    // mUsbThermalPrinter.addStringOffset(320, "320元");
                    // mUsbThermalPrinter.endLine();
                    // mUsbThermalPrinter.addStringOffset(0, "兰州拉面");
                    // mUsbThermalPrinter.addStringOffset(160, "50");
                    // mUsbThermalPrinter.addStringOffset(240, "数量");
                    // mUsbThermalPrinter.addStringOffset(320, "320元");
                    // mUsbThermalPrinter.endLine();
                    // mUsbThermalPrinter.addStringOffset(0, "adbcd");
                    // mUsbThermalPrinter.addStringOffset(160, "50");
                    // mUsbThermalPrinter.addStringOffset(240, "数量");
                    // mUsbThermalPrinter.addStringOffset(320, "320元");
                    // mUsbThermalPrinter.endLine();
                    // mUsbThermalPrinter.addStringOffset(0, "西红柿炒饭");
                    // mUsbThermalPrinter.addStringOffset(160, "50");
                    // mUsbThermalPrinter.addStringOffset(240, "数量");
                    // mUsbThermalPrinter.addStringOffset(320, "320元");
                    // mUsbThermalPrinter.addString("---------------------------");
                    mUsbThermalPrinter.addString(list.get(0).toString());
                    mUsbThermalPrinter.addString("地址：广州市天河区天河北路906高科大厦\n电话：38212803");
                    mUsbThermalPrinter.printString();
                    mUsbThermalPrinter.setAlgin(UsbThermalPrinter.ALGIN_MIDDLE);
                    mUsbThermalPrinter.walkPaper(20);
                    // mUsbThermalPrinter.paperCut();
                    list.remove(0);
                }

            } catch (Exception e) {
                e.printStackTrace();
                // Result = e.toString();
                // if
                // (Result.equals("com.telpo.tps550.api.printer.NoPaperException"))
                // {
                // nopaper = true;
                // } else if
                // (Result.equals("com.telpo.tps550.api.printer.OverHeatException"))
                // {
                // handler.sendMessage(handler.obtainMessage(OVERHEAT, 1, 0,
                // null));
                // } else {
                // handler.sendMessage(handler.obtainMessage(PRINTERR, 1, 0,
                // null));
                // }
            } finally {
                isStartPrint = false;
                // handler.sendMessage(handler.obtainMessage(CANCELPROMPT, 1, 0,
                // null));
                // if (nopaper) {
                // handler.sendMessage(handler.obtainMessage(NOPAPER, 1, 0,
                // null));
                // nopaper = false;
                // return;
                // }
            }
        }
    }






}
